package api

import (
	"ams/dao"
	"ams/service"
	"github.com/gin-gonic/gin"
	"lib/http"
	"lib/structs/ams"
)

var ClientApi = new(clientApi)

type clientApi struct {
}

func (api clientApi) Page(c *gin.Context) {
	var in ams.SearchClientPage
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	//self := http.GetCurrentUser(c)
	li, total, err := service.ClientService.Page(in)
	if err != nil {
		http.BadWithDB(c, err)
		return
	}
	http.OkPage(c, li, total)
}

func (api clientApi) List(c *gin.Context) {
	li, err := dao.ClientDao.List()
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, li)
}

func (api clientApi) LogDetail(c *gin.Context) {
	in := struct {
		LogCode  string `form:"logCode"`
		ClientId int64  `form:"clientId,string"`
	}{}
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	m, err := dao.ClientLogDao.FindDetails(in.ClientId, in.LogCode)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, m)
}
